import MenuIcon from "./MenuIcon"

export function getFirstKey(menus: Menu[]): Menu {
  if (!menus[0].children || !menus[0].children.length) {
    return menus[0]
  }
  return getFirstKey(menus[0].children)
}

export function trans(menus: Menu[]): Item[] {
  return menus
    .filter(item => !item.hideInMenu)
    .map(item => ({
      label: item.name,
      key: item.path,
      icon: item.icon ? <MenuIcon name={item.icon} /> : undefined,
      children: item.children.length && !item.hideChildrenInMenu ? trans(item.children) : undefined,
    }))
}

const NotFound: Menu = {
  name: '404',
  path: '/404',
  component: '/404',
  hideInMenu: true,
  hideChildrenInMenu: true,
  children: [],
  elementAuth: {},
}

const NotAuth: Menu = {
  name: '408',
  path: '/408',
  component: '/408',
  hideInMenu: true,
  hideChildrenInMenu: true,
  children: [],
  elementAuth: {},
}

const generateMenu = (pathname: string, name: string): Menu => ({
  name: name,
  path: pathname,
  component: pathname,
  hideInMenu: true,
  hideChildrenInMenu: true,
  children: [],
  elementAuth: {},
})

const PublicPage: Record<string, Menu> = {
  '/408': generateMenu('/408', '无访问权限'),
  '/system/roleList/edit': generateMenu('/system/roleList/edit', '编辑角色'),
  '/system/managerList/edit': generateMenu('/system/managerList/edit', '编辑员工'),
  '/station/org/edit': generateMenu('/station/org/edit', '编辑经销商'),
  '/station/org/detail': generateMenu('/station/org/detail', '经销商详情'),
  '/station/stationList/edit': generateMenu('/station/stationList/edit', '编辑场站'),
  '/station/shareStationList/edit': generateMenu('/station/shareStationList/edit', '编辑共享场站'),
  '/station/stubList/edit': generateMenu('/station/stubList/edit', '编辑设备'),
  '/coupon/couponList/edit': generateMenu('/coupon/couponList/edit', '添加优惠券'),
  '/coupon/cityVipList/edit': generateMenu('/coupon/cityVipList/edit', '添加城市会员'),
  '/coupon/cardList/edit': generateMenu('/coupon/cardList/edit', '添加现金卡'),
  '/advertisement/list/edit': generateMenu('/advertisement/list/edit', '添加广告'),
  '/normal/normalList/components/edit': generateMenu('/normal/normalList/components/edit', '手动退款'),
  '/normal/normalList/detail': generateMenu('/normal/normalList/detail', '用户详情'),
  '/bigC/bigCList/edit': generateMenu('/bigC/bigCList/edit', '新建企业'),
  '/order/chargeOrder/detail': generateMenu('/order/chargeOrder/detail', '充电订单详情'),
  '/moneySettle/orgSettle/detail': generateMenu('/moneySettle/orgSettle/detail', '结算单详情'),
  '/moneySettle/invoice/detail': generateMenu('/moneySettle/invoice/detail', '发票详情'),
  '/station/watch/detail': generateMenu('/station/watch/detail', '监控详情'),
  '/companyOrder/detail': generateMenu('/companyOrder/detail', '订单详情'),
  '/mall/physical/goods/add': generateMenu('/mall/physical/goods/add', '新建商品'),
  '/mall/logistics/template/edit': generateMenu('/mall/logistics/template/edit', '新建物流模板'),
  '/order/mall/physical/detail': generateMenu('/order/mall/physical/detail', '订单详情'),
  '/order/mall/points/detail': generateMenu('/order/mall/points/detail', '订单详情'),
  '/cooperation': generateMenu('/cooperation', '合作建站'),
  '/zhaoMu/edit': generateMenu('/zhaoMu/edit', '招募详情'),
  '/station/cityMList/edit': generateMenu('/station/cityMList/edit', '编辑城市经理人'),
}

export function getMenu(menus: Menu[], pathname: string): Menu {
  if (!pathname || pathname === '/') {
    return getFirstKey(menus)
  }

  if (PublicPage[pathname]) {
    return PublicPage[pathname]
  }

  const map = (menus: Menu[]): Menu | undefined => {
    let final = true
    const menu = menus.find(item => {
      if (pathname === item.path) return true
      if (pathname.startsWith(item.path) && item.children?.length) {
        final = false
        return true
      }
      return false
    })
    if (!final) {
      return map(menu!.children)
    }
    return menu
  }

  return map(menus) || NotFound
}

export const getOpenKeys = (pathname?: string): string[] => {
  const result: string[] = []

  if (!pathname) return result

  const tier = pathname.split('/')
  tier.shift()
  if (tier.length === 1) return result

  tier.pop()
  while(tier.length > 1) {
    result.push(`/${tier.join('/')}`)
    tier.pop()
  }
  result.push(`/${tier.join('/')}`)
  
  return result
}